﻿@{
    ViewBag.Title = "选择用户";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}


<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initOrgTable();
        initUserTable();
    });

    function initOrgTable(queryData) {
        $('#orgTreeGrid').treegrid({
            url: '/OrgInfo/GetDataList?exceptAll=True',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            treeField: 'OrgName',
            parentField: 'PID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            columns: [[
                { title: '机构名称', field: 'OrgName', width: 200, sortable: true },
            ]],
            onClickRow: function (index, row) {
                //以下方法可行
                var selectedRow = $("#orgTreeGrid").treegrid("getSelected");
                if (selectedRow) {
                    $('#userDataGrid').datagrid('load', { orgID: selectedRow.ID });
                }
            }
        });
    }

    //初始化用户表
    function initUserTable(queryData) {
        $('#userDataGrid').datagrid({
            url: '/Purview/GetUserDataListNoPage?orgID=unknow',  //首次不进行加载
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            columns: [[
                {
                    field: 'IsChecked',
                    formatter: function (value, row, index) {
                        if (value == true) {
                            return '<input type="checkbox" checked="true" onclick="ChooseUser.ChangeUserChecked(' + index + ');" />';
                        }
                        else {
                            return '<input type="checkbox" onclick="ChooseUser.ChangeUserChecked(' + index + ');" />';
                        }
                    }
                },
                { title: '用户名', field: 'UserName', width: 120, sortable: true },
                { title: '姓名', field: 'RealName', width: 80, sortable: true }
            ]],
            //onClickRow: function (index, row) {
            //    //以下方法可行
            //    var selectedRow = $("#userDataGrid").datagrid("getSelected");
            //    if (selectedRow) {
            //        //$("#hSelectedUserID") = selectedRow.ID;  //此方法不可行
            //        document.getElementById("hSelectedUserID").value = selectedRow.ID;
            //        document.getElementById("hSelectedUserName").value = selectedRow.UserName;
            //    }
            //}
        });
    }
    var ChooseUser = {
        ////刷新机构
        //ReloadOrg: function () {
        //    $("#orgTreeGrid").treegrid("reload");
        //},

        ////刷新用户
        //ReloadUser: function () {
        //    $("#userDataGrid").datagrid("reload");
        //}

        //勾选或者取消勾选
        ChangeUserChecked: function (index) {
            //勾选或取消勾选的后台事件
            $("#userDataGrid").datagrid("selectRow", index);  //选中指定行
            var selectedRow = $("#userDataGrid").datagrid("getSelected");  //获取选中行记录
            if (selectedRow) {
                //此处是为了取出IsChecked的值
                if (selectedRow.IsChecked == false) {
                    selectedRow.IsChecked = true;
                }
                else {
                    selectedRow.IsChecked = false;
                }
                $.post("/Purview/ChangeUserChecked?userOrgID=" + selectedRow.UserOrgID + "&isChecked=" + selectedRow.IsChecked);  //xxxx由上面的方法获取
            }

            //给隐藏域赋值
            //var userOrgIDs = "";
            //var selectedOrgList = $('#userDataGrid').datagrid('getSelections');  //获取列表数据
            //if (selectedOrgList.length > 0) {
            //    for (var i = 0; i < selectedOrgList.length; i++) {
            //        //如果已勾选
            //        if (selectedOrgList[i].IsChecked) {
            //            if (userOrgIDs.length == 0)
            //                userOrgIDs = selectedOrgList[i].ID;
            //            else
            //                userOrgIDs = userOrgIDs + "," + selectedOrgList[i].ID;
            //        }
            //    }
            //}
            //debugger;
            ////$("#hOrgIDs").val() = orgIDs;
            //document.getElementById("hOrgIDs").value = orgIDs;  //给隐藏域字段赋值
        }
    }
</script>

<form id="form-chooseUser" method="post">
    <table>
        <tr>
            <td>
                <div region="west" data-options="border:false" style="width: 250px; height: 400px;">
                    <div class="easyui-panel" title="机构列表">
                        <table id="orgTreeGrid" style="height: 358px;"></table>
                    </div>
                </div>
            </td>
            <td>
                <div region="center" style="border-left: 0px; width: 255px; height: 400px;">
                    <div class="easyui-panel" title="用户列表">
                        <form id="form-purview" method="post">
                            <table id="userDataGrid" style="height: 358px;"></table>
                        </form>
                    </div>
                </div>
            </td>
        </tr>
    </table>
</form>
@*<input type="hidden" id="hSelectedUserID" />
<input type="hidden" id="hSelectedUserName" />*@

<input type="hidden" id="hSelectedUserOrgIDs" value="@ViewBag.SelectedUserOrgIDs" />